package vn.sms.lazylove.adapter;

import java.util.ArrayList;
import java.util.List;

import vn.sms.lazylove.R;
import vn.sms.lazylove.gui.ContactAvatarLoader;
import vn.sms.lazylove.gui.CountSelectContactListener;
import vn.sms.lazylove.object.Contact;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ContactAdapter extends BaseAdapter {

	private Activity mActivity;
	private LayoutInflater mInflater;
	private ContactAvatarLoader mImageContactLoader;
	private List<Contact> mListContacts = new ArrayList<Contact>();
	private List<Contact> mSelectListContacts = new ArrayList<Contact>();
	private static CountSelectContactListener listener;

	public static void setCountSelectedContactListener(
			CountSelectContactListener _listener) {
		listener = _listener;
	}

	public static void removeCountSelectedContactListener() {
		listener = null;
	}

	public ContactAdapter(Activity _activity, List<Contact> _listcontacs) {
		mActivity = _activity;
		mInflater = LayoutInflater.from(_activity);
		mImageContactLoader = ContactAvatarLoader.getInstance(_activity);
		if (mListContacts != null)
			mListContacts = _listcontacs;
	}

	public void setData(List<Contact> objs) {
		mListContacts = objs;
	}

	public List<Contact> getSelectListContacts() {
		return mSelectListContacts;
	}

	public String getListSelectedPhone() {
		String phones = "";
		if (mSelectListContacts.size() > 0)
			for (Contact contact : mSelectListContacts) {
				phones = phones + contact.Phone + ",";
			}
		if (!phones.equals(""))
			phones = phones.substring(0, phones.length() - 1);
		return phones;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mListContacts.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mListContacts.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		Object obj = mListContacts.get(position);
		final Contact contact = (Contact) obj;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_listview_contact,
					null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.txtName);
			holder.name.setSelected(true);
			holder.phone = (TextView) convertView.findViewById(R.id.txtPhone);
			holder.checkbox = (CheckBox) convertView
					.findViewById(R.id.checkbox);
			holder.avatar = (ImageView) convertView
					.findViewById(R.id.img_avatar);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (obj instanceof Contact) {
			holder.name.setText(contact.Name);
			holder.phone.setText(contact.Phone);
			mImageContactLoader.DisplayImage(contact.Phone, mActivity,
					holder.avatar, ContactAvatarLoader.user_image_id);
		}

		holder.checkbox.setOnCheckedChangeListener(null);
		if (mSelectListContacts.contains(contact))
			holder.checkbox.setChecked(true);
		else
			holder.checkbox.setChecked(false);

		convertView.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (holder.checkbox.isChecked()) {
					holder.checkbox.setChecked(false);
				} else {
					holder.checkbox.setChecked(true);
				}
			}
		});

		holder.checkbox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked)
							mSelectListContacts.add(contact);
						else
							mSelectListContacts.remove(contact);

						if (listener != null) {
							listener.onSelectedContactChanged(mSelectListContacts
									.size());
						}

					}
				});

		return convertView;
	}

	class ViewHolder {
		TextView name;
		TextView phone;
		CheckBox checkbox;
		ImageView avatar;
	}

}
